package com.card.vault.domain.user;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.*;

import com.card.vault.domain.another.Card;

@NamedQueries({
	@NamedQuery(name = User.NamedQuery.USER_FIND_ALL, 	query = "from User"),
	@NamedQuery(name = User.NamedQuery.USER_FIND_BY_ID, query = "from User where id = :id") })
@NamedNativeQueries({ 
	@NamedNativeQuery(name = User.NamedQuery.USER_FIND_BY_NAME, query = "select * from User where USERNAME like :name", resultClass = User.class) })
@Entity
public class User implements Serializable {
	
	private static final long serialVersionUID = -6398119192803522924L;

	@Id
	private int id;

	@Column(name = "USERNAME", nullable = false)
	private String name;

	@Column(name = "PASSWORD", nullable = false)
	private String password;
	
	@Column(name = "CELLNUMBER")
	private String cellNumber;
	
	@Column(name = "AUTHORITY", columnDefinition = "enum('ROLE_USER','ROLE_ADMIN')")
	@Enumerated(EnumType.STRING)
	private Authority authority = Authority.ROLE_USER;
	
	@Column(name = "ENABLED", columnDefinition = "TINYINT", length = 1)
	private boolean enabled = true;
	
	@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER)
	private List<Card> card = new ArrayList<Card>();
	
	
	public User() {}
	public User(String name, String password, String cellNumber) {
		this.name = name;
		this.password = password;
		this.cellNumber = cellNumber;
		authority = Authority.ROLE_USER;
		enabled = true;
	}


	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getCellNumber() {
		return cellNumber;
	}
	public void setCellNumber(String cellNumber) {
		this.cellNumber = cellNumber;
	}
	public Authority getAuthority() {
		return authority;
	}
	public void setAuthority(Authority authority) {
		this.authority = authority;
	}
	public boolean isEnabled() {
		return enabled;
	}
	public void setEnabled(boolean enabled) {
		this.enabled = enabled;
	}
	public List<Card> getCard() {
		return card;
	}
	public void setCard(List<Card> card) {
		this.card = card;
	}


	public static class NamedQuery {
		public static final String USER_FIND_ALL 	 = "User.findAll";
		public static final String USER_FIND_BY_ID 	 = "User.findById";
		public static final String USER_FIND_BY_NAME = "User.findByName";
	}

}